package com.springboot.quanxian.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.quanxian.common.DictionaryEnum;
import com.springboot.quanxian.common.ResultEnum;
import com.springboot.quanxian.entity.SysMenu;
import com.springboot.quanxian.entity.SysRole;
import com.springboot.quanxian.entity.req.SysRoleReq;
import com.springboot.quanxian.entity.search.SysRoleSearch;
import com.springboot.quanxian.entity.view.SysRoleView;
import com.springboot.quanxian.exception.MyException;
import com.springboot.quanxian.mapper.SysRoleMapper;
import com.springboot.quanxian.qpage.QPage;
import com.springboot.quanxian.service.ISysMenuService;
import com.springboot.quanxian.service.ISysRoleMenuService;
import com.springboot.quanxian.service.ISysRoleService;
import com.springboot.quanxian.util.BuildTree;
import com.springboot.quanxian.util.IdentityUtil;
import com.springboot.quanxian.util.ResultView;
import com.springboot.quanxian.util.Tree;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lbk
 * @since 2023-04-17
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;

    @Autowired
    private ISysMenuService iSysMenuService;



    @Override
    public ResultView getPageList(QPage qPage, SysRoleSearch obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        List<SysRoleView> mapList = this.baseMapper.getPageList(iPage,obj);
        return ResultView.ok(iPage.setRecords(mapList));
    }


    @Transactional
    @Override
    public ResultView add(SysRoleReq obj) {
        if(StringUtils.isEmpty(obj.getRoleName())){
            throw new MyException(ResultEnum.CODE_2,"角色名称不能为空");
        }
        SysRole dto=new SysRole();
        BeanUtils.copyProperties(obj,dto);
        dto.setRoleId(IdentityUtil.identityId("RO"));
        dto.setAddTime(new Date());
        dto.setAddManagerId(StpUtil.getLoginIdAsString());
        boolean isOk=this.save(dto);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"新增角色数据异常");
        }
        //分配菜单
        this.iSysRoleMenuService.saveRoleMenu(obj.getMenuId(),dto.getRoleId());
        return ResultView.ok();
    }

    @Transactional
    @Override
    public ResultView del(String roleId) {
        SysRole dto=this.getById(roleId);
        if(dto==null){
            throw new MyException(ResultEnum.CODE_2,"角色数据不存在");
        }
        dto.setIsDel(DictionaryEnum.ISDEL_TWO.getCode());
        boolean isOk=this.updateById(dto);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"删除角色数据异常");
        }
        return ResultView.ok();
    }

    @Transactional
    @Override
    public ResultView batchDel(SysRoleReq obj) {
        if(obj.getRoleIds()==null||obj.getRoleIds().length==0){
            throw new MyException(ResultEnum.CODE_2,"角色ID参数ID异常");
        }
        String[] roleIds=obj.getRoleIds();
        for (String roleId : roleIds) {
            this.del(roleId);
        }
        return ResultView.ok();
    }

    @Transactional
    @Override
    public ResultView up(SysRoleReq obj) {
        if(StringUtils.isEmpty(obj.getRoleId())){
            throw new MyException(ResultEnum.CODE_2,"角色ID不能为空");
        }
        if(StringUtils.isEmpty(obj.getRoleName())){
            throw new MyException(ResultEnum.CODE_2,"角色名称不能为空");
        }
        SysRole dto=this.getById(obj.getRoleId());
        if(dto==null){
            throw new MyException(ResultEnum.CODE_2,"角色数据不存在");
        }
        BeanUtils.copyProperties(obj,dto);
        dto.setUpdateTime(new Date());
        dto.setUpdateManagerId(StpUtil.getLoginIdAsString());
        boolean isOk=this.updateById(dto);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"更新角色数据异常");
        }
        //分配菜单
        this.iSysRoleMenuService.saveRoleMenu(obj.getMenuId(),dto.getRoleId());
        return ResultView.ok();
    }

    @Override
    public SysRole exitName(String name) {
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("role_name", name);
        queryWrapper.eq("is_del", DictionaryEnum.ISDEL_ONE.getCode());
        SysRole dto=this.getOne(queryWrapper);
        return dto;
    }

    @Override
    public List<SysRole> getSelectList() {
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_del",DictionaryEnum.ISDEL_ONE.getCode());
        List<SysRole> roleList=this.list(queryWrapper);
        return roleList;
    }

    @Override
    public ResultView roleMenuList(String roleId) {
        Map<String,Object> map=new HashMap<>();
        List<Tree<SysMenu>> trees = new ArrayList<Tree<SysMenu>>();
        //获取所有菜单集合
        List<SysMenu> menuList=this.iSysMenuService.getListAll(null);
        //当前角色绑定的菜单
        if(org.apache.commons.lang3.StringUtils.isNotBlank(roleId)){
            List<Object> roleMenus=this.iSysRoleMenuService.getByRoleId(roleId);
            map.put("menuId",roleMenus);
        }
        for (SysMenu sysMenuDO : menuList) {
            Tree<SysMenu> tree = new Tree<SysMenu>();
            tree.setId(sysMenuDO.getMenuId());
            tree.setParentId(sysMenuDO.getParentId());
            tree.setText(sysMenuDO.getMenuName());
            tree.setPrem(sysMenuDO.getMenuPerm());
            tree.setMenuUrl(sysMenuDO.getMenuUrl());
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        List<Tree<SysMenu>> list = BuildTree.buildList(trees, "");
        map.put("menu",list);
        return ResultView.ok(map);
    }

    @Override
    public SysRoleView getByIds(String roleId) {
        SysRole sysRole=this.getById(roleId);
        SysRoleView sysRoleView=new SysRoleView();
        BeanUtils.copyProperties(sysRole,sysRoleView);
        return sysRoleView;
    }


}
